package com.kaki.mdb;

import org.springframework.core.io.ClassPathResource;
import java.io.IOException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;


/**
 * @author Kaki Nakajima
 * @description DB Show 入口
 * @date 2024-09-17 10:11
 */
public class DBShowDomain implements ActionListener {

    private static JTextField ipField = null;
    private static JTextField usernameField = null;
    private static JTextField passwordField = null;
    private static JTextField portField = null;
    private static JTextField nickNameField = null;
    private static JFrame window1 = null;

    public static Font FONT_18 = new Font("微软雅黑",Font.LAYOUT_NO_LIMIT_CONTEXT,18);
    public static Font FONT_12 = new Font("微软雅黑",Font.LAYOUT_NO_LIMIT_CONTEXT,12);

    // 入口程序
    public static void main(String[] args) throws IOException {

        // 标题
        JLabel ip = new JLabel("mdb/sqlite文件所在路径：");
        ip.setFont(FONT_18);

        JLabel dbType = new JLabel("数据库类型：");
        dbType.setFont(FONT_18);

        // 历史记录框
        Box panelBox = Box.createHorizontalBox();
        panelBox.setBounds(640,30,200,900);

        final JPanel themePanel = new JPanel();
        themePanel.setBackground(new Color(204,238,200));
        themePanel.setSize(40,20);

        // 历史记录label
        Box historyLabelBox = Box.createVerticalBox();
        historyLabelBox.setBounds(650,1,200,900);

        panelBox.add(themePanel);

        // 输入框组
        nickNameField = new JTextField();
        ipField = new JTextField();
        usernameField = new JTextField();
        passwordField = new JTextField();
        portField = new JTextField();

        // 选择框
        final JComboBox dbTypeSelector = new JComboBox();
        dbTypeSelector.setFont(FONT_12);
        dbTypeSelector.addItem("--请选择--");
        dbTypeSelector.addItem("MDB");
        dbTypeSelector.addItem("Sqlite");

        // 按钮组
        JButton scan = new JButton("启动");
        scan.setFont(FONT_18);

        // 盒子模型组
        Box buttonBox = Box.createHorizontalBox();
        Box ipBox = Box.createHorizontalBox();
        Box nickNameBox = Box.createHorizontalBox();
        Box dbTypeBox = Box.createHorizontalBox();
        Box passwordBox = Box.createHorizontalBox();
        Box portBox = Box.createHorizontalBox();

        // ip 输入框
        ipBox.add(ip);
        ipBox.add(Box.createHorizontalStrut(26));
        ipBox.add(ipField);

        //选择框
        dbTypeBox.add(dbType);
        dbTypeBox.add(Box.createHorizontalStrut(80));
        dbTypeBox.add(dbTypeSelector);

        ipBox.add(new JScrollPane(ipField){
            @Override
            public Dimension getPreferredSize(){
                return new Dimension(50,50);
            }
        });

        // 总按钮盒子
        buttonBox.add(Box.createHorizontalStrut(180));

        scan.setBackground(Color.magenta);

        buttonBox.add(Box.createVerticalStrut(111));
        buttonBox.add(scan);

        //点击按钮事件
        scan.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String path = ipField.getText();
                String dbType = dbTypeSelector.getSelectedItem().toString();

                if ((!"MDB".equals(dbType)) && !"Sqlite".equals(dbType)){
                    try {
                        AlertFrame.alert("数据库类型选择错误！！");
                        return;
                    }catch (Exception ex){
                        System.out.println("==========>7");
                        ex.printStackTrace();
                    }
                }

                HashMap<String, Object> userMap = new HashMap<String, Object>();
                userMap.put("path",path);
                userMap.put("dbType",dbType);

                try {
                    // 关闭当前窗口
                    window1.dispose();

                    // 开启数据控制台窗口
                    DBShowJDBCHandler.DBShowStrat(userMap);

                }catch (Exception ex2){
                    System.out.println("==========>8");
                    ex2.printStackTrace();
                }
            }
        });

        // 总弹框
        window1 = new JFrame("DB Show");
        Container container = window1.getContentPane();
        container.setBackground(new Color(204,238,200));
        window1.setBounds(60,100,800,460);
        window1.setVisible(true);
        window1.setLayout(null);

        ClassPathResource classPathResource = new ClassPathResource("/photos/main.png");

        // 本地图片
        ImageIcon imageIcon = new ImageIcon(classPathResource.getURL());
        window1.setIconImage(imageIcon.getImage());

        //页面位置设置
        nickNameBox.setBounds(20,35,600,40);
        ipBox.setBounds(20,90,600,40);
        dbTypeBox.setBounds(20,145,600,40);
        passwordBox.setBounds(20,200,600,40);
        portBox.setBounds(20,252,600,40);
        buttonBox.setBounds(20,310,350,40);

        nickNameField.setFont(FONT_18);
        ipField.setFont(FONT_18);
        usernameField.setFont(FONT_18);
        passwordField.setFont(FONT_18);
        portField.setFont(FONT_18);

        window1.add(historyLabelBox);
        window1.add(panelBox);
        window1.add(nickNameBox);
        window1.add(ipBox);
        window1.add(dbTypeBox);
        window1.add(passwordBox);
        window1.add(portBox);
        window1.add(buttonBox);
        window1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        window1.validate();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}
